package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.Problem;
import com.aftersale.demo.entity.Result;
import com.aftersale.demo.utils.FileUpload;
import com.aftersale.demo.utils.MyUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.entity.Solve;
import com.aftersale.demo.service.SolveService;
import com.aftersale.demo.mapper.SolveMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 *
 */
@Service
public class SolveServiceImpl extends ServiceImpl<SolveMapper, Solve>
    implements SolveService{

    @Autowired
    SolveMapper solveMapper;

    @Override
    public Result getMySolve(int problemId) {
        QueryWrapper<Solve> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("problem_id",problemId);
        Solve solve=getOne(queryWrapper);
        if(Objects.isNull(solve)){
            return Result.error("401","问题待解决");
        }
        return Result.success(solve);
    }

    @Override
    public Result addSolve(Solve solve) {
        QueryWrapper<Solve> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("problem_id",solve.getProblemId());
        Solve dbSolve=getOne(queryWrapper);
        if(!Objects.isNull(dbSolve)){
            return Result.error("401","解决方案已提交，请勿重复提交");
        }
        if(!Objects.isNull(solve.getFile())){
            if(!solve.getFile().isEmpty()){
                String url = FileUpload.upload(solve.getFile());
                solve.setUrl(url);
            }
        }
        solveMapper.insert(solve);
        return Result.success("提交成功");
    }

    @Override
    public Result updateSolve(Solve solve) {
        if(!Objects.isNull(solve.getFile())){
            if(!solve.getFile().isEmpty()){
                String url = FileUpload.upload(solve.getFile());
                solve.setUrl(url);
            }
        }
        solveMapper.updateById(solve);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteSolve(int solveId) {
        Solve dbSolve =solveMapper.selectById(solveId);
        if(Objects.isNull(dbSolve)){
            return Result.error("401","该解决方案不存在");
        }else{
            solveMapper.deleteById(solveId);
            return Result.success("删除成功");
        }
    }
}




